const express = require('express');
const router = express.Router();
const packageService = require('../services/PackageService');
const tagService = require('../services/TagService');
const statsService = require('../services/StatsService');

// 获取包的下载趋势
router.get('/downloads/:packageId', async (req, res) => {
  try {
    const { packageId } = req.params;
    const { days = 30 } = req.query;
    const stats = await packageService.getDownloadTrend(packageId, days);
    res.json(stats);
  } catch (error) {
    console.error('获取下载趋势失败:', error);
    res.status(500).json({ error: error.message || '获取下载趋势失败' });
  }
});

// 获取每日热门包
router.get('/trending/daily', async (req, res) => {
  try {
    // 注意：这个功能在当前的Service中没有直接实现，需要在Service中添加
    // 这里只是一个简单的实现，实际应该从npm API获取最新数据
    const today = new Date();
    today.setHours(0, 0, 0, 0);
    
    // 由于缺少每日下载数据的具体实现，这里返回热门包列表
    const popularPackages = await packageService.getPopularPackages(20);
    
    // 模拟每日数据
    const result = popularPackages.map(pkg => ({
      package: pkg,
      downloads: pkg.downloads,
      date: today
    }));
    
    res.json(result);
  } catch (error) {
    console.error('获取每日热门包失败:', error);
    res.status(500).json({ error: error.message || '获取每日热门包失败' });
  }
});

// 获取总排行榜
router.get('/leaderboard/total', async (req, res) => {
  try {
    const packages = await packageService.getLeaderboard(100);
    res.json(packages);
  } catch (error) {
    console.error('获取总排行榜失败:', error);
    res.status(500).json({ error: error.message || '获取总排行榜失败' });
  }
});

// 获取标签排行榜
router.get('/leaderboard/tags', async (req, res) => {
  try {
    const tags = await tagService.getTagLeaderboard(50);
    res.json(tags);
  } catch (error) {
    console.error('获取标签排行榜失败:', error);
    res.status(500).json({ error: error.message || '获取标签排行榜失败' });
  }
});

// 获取标签下的包排行榜
router.get('/leaderboard/tag/:tagId', async (req, res) => {
  try {
    const { tagId } = req.params;
    const packages = await tagService.getPackageLeaderboardByTag(tagId, 50);
    res.json(packages);
  } catch (error) {
    console.error('获取标签下的包排行榜失败:', error);
    res.status(500).json({ error: error.message || '获取标签下的包排行榜失败' });
  }
});

// 获取统计概览
router.get('/overview', async (req, res) => {
  try {
    const { timeRange, startDate, endDate } = req.query;
    const stats = await statsService.getOverviewStats({ timeRange, startDate, endDate });
    res.json(stats);
  } catch (error) {
    console.error('获取统计概览失败:', error);
    res.status(500).json({ error: error.message || '获取统计概览失败' });
  }
});

// 获取下载趋势
router.get('/download-trend', async (req, res) => {
  try {
    const { timeRange, startDate, endDate } = req.query;
    const trend = await statsService.getDownloadTrend({ timeRange, startDate, endDate });
    res.json(trend);
  } catch (error) {
    console.error('获取下载趋势失败:', error);
    res.status(500).json({ error: error.message || '获取下载趋势失败' });
  }
});

// 获取热门包
router.get('/top-packages', async (req, res) => {
  try {
    const { timeRange, startDate, endDate, limit = 10 } = req.query;
    const packages = await statsService.getTopPackages({ timeRange, startDate, endDate, limit });
    res.json(packages);
  } catch (error) {
    console.error('获取热门包失败:', error);
    res.status(500).json({ error: error.message || '获取热门包失败' });
  }
});

// 获取标签分布
router.get('/tag-distribution', async (req, res) => {
  try {
    const distribution = await statsService.getTagDistribution();
    res.json(distribution);
  } catch (error) {
    console.error('获取标签分布失败:', error);
    res.status(500).json({ error: error.message || '获取标签分布失败' });
  }
});

// 获取讨论活跃度
router.get('/discussion-activity', async (req, res) => {
  try {
    const { timeRange, startDate, endDate } = req.query;
    const activity = await statsService.getDiscussionActivity({ timeRange, startDate, endDate });
    res.json(activity);
  } catch (error) {
    console.error('获取讨论活跃度失败:', error);
    res.status(500).json({ error: error.message || '获取讨论活跃度失败' });
  }
});

// 获取详细下载统计
router.get('/download-details', async (req, res) => {
  try {
    const { timeRange, startDate, endDate } = req.query;
    const details = await statsService.getDownloadDetails({ timeRange, startDate, endDate });
    res.json(details);
  } catch (error) {
    console.error('获取详细下载统计失败:', error);
    res.status(500).json({ error: error.message || '获取详细下载统计失败' });
  }
});

module.exports = router;